﻿using Microsoft.AspNet.Identity;
using Microsoft.Owin.Security;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Security.Principal;
using System.Web;

namespace Inovout.Security
{
    public static class IdentityExtensions
    {
        public static int[] GetCompanyIds(this IIdentity identity)
        {
            ClaimsIdentity claimsIdentity = identity as ClaimsIdentity;
            Claim companyClaim = claimsIdentity.FindFirst(Constants.CompanyClaimType);
            var companyJson = (companyClaim != null) ? companyClaim.Value : "";
            JArray jarry = (JArray)JsonConvert.DeserializeObject(companyJson);
            List<int> listCompanyIds = new List<int>();
            int[] companyIds = { };
            if (jarry != null)
            {
                foreach (var obj in jarry)
                {
                    listCompanyIds.Add(Convert.ToInt16(((JObject)obj)["id"]));
                }
            }
            companyIds = listCompanyIds.ToArray();
            return companyIds;
        }


        public static string GetPersonId(this IIdentity identity)
        {
            ClaimsIdentity claimsIdentity = identity as ClaimsIdentity;
            Claim personClaim = claimsIdentity.FindFirst(Constants.PersonClaimType);
            string personId = (personClaim != null) ? personClaim.Value : "";
            return personId;
        }

        public static string GetMembershipName(this IIdentity identity)
        {
            ClaimsIdentity claimsIdentity = identity as ClaimsIdentity;
            Claim membershipClaim = claimsIdentity.FindFirst(Constants.NameClaimType);
            string membershipName = (membershipClaim != null) ? membershipClaim.Value : "";
            return membershipName;
        }

        public static void AddCompanyId(this IIdentity identity, int companyId)
        {

            ClaimsIdentity claimsIdentity = identity as ClaimsIdentity;
            Claim companyClaim = claimsIdentity.FindFirst(Constants.CompanyClaimType);
            claimsIdentity.RemoveClaim(companyClaim);
            var companyJson = (companyClaim != null) ? companyClaim.Value : "";
            JArray jarry = (JArray)JsonConvert.DeserializeObject(companyJson);
            List<int> listCompanyIds = new List<int>();
            if (jarry != null)
            {
                foreach (var obj in jarry)
                {
                    listCompanyIds.Add(Convert.ToInt16(((JObject)obj)["id"]));
                }
            }
            List<object> listCompanys = new List<object>();
            foreach (int comId in listCompanyIds)
            {
                listCompanys.Add(new { name = "", id = comId });
            }
            if (!listCompanyIds.Contains(companyId))
            {
                listCompanys.Add(new { name = "", id = companyId });

            }

            claimsIdentity.AddClaim(new Claim(Constants.CompanyClaimType, JsonConvert.SerializeObject(listCompanys), Constants.XmlSchemaString, DefaultAuthenticationTypes.ApplicationCookie));

            //退出
            IAuthenticationManager AuthenticationManager = HttpContext.Current.GetOwinContext().Authentication;

            AuthenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie);

            AuthenticationManager.SignIn(
                new AuthenticationProperties { IsPersistent = false }, claimsIdentity);

        }
    }
}
